<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="所属分区" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classifyId">
              <j-search-select-tag ref="classifyIdRef" type="list" v-model="model.classifyId" placeholder="请选择分区" @change="classifyChange" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="国家或地区" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="countryId">
              <j-search-select-tag ref="countryIdRef" type="list" v-model="model.countryId" placeholder="请选择国家或地区" @change="countryChange" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="城市" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cityId">
              <j-search-select-tag ref="cityIdRef" type="list" v-model="model.cityId" placeholder="请选择国家或地区" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="开始邮编" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startZip">
              <a-input v-model.trim="model.startZip" placeholder="请输入开始邮编"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="结束邮编" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endZip">
              <a-input v-model.trim="model.endZip" placeholder="请输入结束邮编"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'LogisticsPartitionCityForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        searchDictParam:{
          partitionId:''
        },
        validatorRules: {
          classifyId: [
            { required: true, message: '请选择所属分区!'},
          ],
          countryId: [
            { required: true, message: '请选择国家或地区!'},
          ],
        },
        url: {
          add: "/logistics/part/logisticsPartitionCity/add",
          edit: "/logistics/part/logisticsPartitionCity/edit",
          queryById: "/logistics/part/logisticsPartitionCity/queryById",
          queryAllByParam: "/logistics/part/logisticsPartitionClassify/queryAllByParam",
          queryCountryByClassify: "/logistics/part/logisticsPartitionCountry/queryCountryByClassify",
          queryCityByCountry: "/logistics/part/logisticsPartitionCity/queryCityByCountry"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    watch: {
      'searchDictParam.partitionId': {
        handler(val){
          this.getQueryAllByParam();
        }
      }
    },
    methods: {
      add (partitionId) {
        this.searchDictParam.partitionId=partitionId;
        this.modelDefault.partitionId=partitionId;
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.searchDictParam.partitionId=record.partitionId;
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      getCountry(){
        getAction(`/logistics/part/logisticsPartitionCountry/queryAllByParam`,{"keyword":value, classifyId: this.model.classifyId}).then(res=>{
          this.loading=false
          if(res.success){
            if(currentLoad!=this.lastLoad){
              return
            }
            this.options = res.result
          }else{
            this.$message.warning(res.message)
          }

        })
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      getQueryAllByParam(){
        this.$refs.classifyIdRef.setCurrentDictOptions([]);
        getAction(this.url.queryAllByParam, {partitionId: this.searchDictParam.partitionId}).then((res) => {
          if(res.success){
            let dictOptions = res.result && res.result.map(item => {
              return {
                value: item.id,
                text: item.name
              }
            })
            this.$refs.classifyIdRef.setCurrentDictOptions(dictOptions);
          }else{
            this.$message.warning(res.message);
          }
        })
      },
      classifyChange(){
        this.$refs.countryIdRef.setCurrentDictOptions([]);
        this.$refs.cityIdRef.setCurrentDictOptions([]);
        this.$set(this.model, 'countryId', '');
        this.$set(this.model, 'cityId', '');
        getAction(this.url.queryCountryByClassify, {classifyId: this.model.classifyId}).then((res) => {
          if(res.success){
           let dictOptions = res.result && res.result.map(item => {
              return {
                value: item.id,
                text: item.cnName
              }
            })
            this.$refs.countryIdRef.setCurrentDictOptions(dictOptions);
          }else{
            this.$message.warning(res.message);
          }
        })
      },
      countryChange(){
        this.$refs.cityIdRef.setCurrentDictOptions([]);
        this.$set(this.model, 'cityId', '');
        getAction(this.url.queryCityByCountry, {countryId: this.model.countryId}).then((res) => {
          if(res.success){
            let dictOptions = res.result && res.result.map(item => {
              return {
                value: item.id,
                text: item.cnName
              }
            })
            this.$refs.cityIdRef.setCurrentDictOptions(dictOptions);
          }else{
            this.$message.warning(res.message);
          }
        })
      }
    }
  }
</script>